package com.project.service.impl;

import com.project.dao.MapDOMapper;
import com.project.dataobject.MapDO;
import com.project.service.LinkService;
import com.project.util.ConvertDateUtils;
import com.project.util.ConvertUtils;
import com.project.util.SnowflakeDistributedId;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.rmi.UnexpectedException;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.TimeUnit;


@Slf4j
@Service
public class LinkServiceImpl implements LinkService {

    /**
     * 生成短链接次数的key
     */
    private String CUR_SIZE = "cur_size";

    /**
     * 短链接解析次数的key
     */
    private String USED_SIZE = "used_size";

    /**
     * 当天的短链接生成次数的key前缀
     */
    private String TODAY_CUR_SIZE = "day_cur_size:";

    /**
     * 当天的短链接解析次数的key前缀
     */
    private String TODAY_USED_SIZE = "day_used_size:";

    private  long EXPIRE_TIME = 9;


    @Autowired
    private MapDOMapper mapDOMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 短链接生成服务
     * @param longUrlList 输入长链接列表
     * @return 返回短链接列表
     * @throws UnexpectedException
     */
    @Transactional
    @Override
    public List<String> getShortUrl(List<String> longUrlList) throws UnexpectedException {
        SnowflakeDistributedId id = new SnowflakeDistributedId(Instant.now().toEpochMilli(),
                5, 1, 8);
        // 存储结果集
        List<String> result = new ArrayList<>();

        for (int i = 0; i<longUrlList.size();i++){
            // 检查该长链接是否已经生成过
            MapDO mapDO = mapDOMapper.selectByLongUrl(longUrlList.get(i));
            // map存储映射关系
            Map<String,String> map = new HashMap<>();
            // 不为空则记录结果
            if (mapDO != null){
                result.add(mapDO.getShortUrl());
                continue;
            }
            // 生成字符串组合
            String base62 = ConvertUtils.toOtherNumberSystem(id.generate(),62);
            result.add(base62);
            // 将映射关系存进redis
            redisTemplate.opsForValue().set(base62,longUrlList.get(i));
            // 存进数据库
            MapDO mapDO1 = new MapDO();
            mapDO1.setShortUrl(base62);
            mapDO1.setLongUrl(longUrlList.get(i));
            mapDOMapper.insertSelective(mapDO1);
        }
        // 拼接key值
        String todayKey = TODAY_CUR_SIZE + ConvertDateUtils.getDate();
        // 记录总个数
        redisTemplate.opsForValue().increment(CUR_SIZE,result.size());
        // 记录当天的次数 有效时间为9天
        // 初始化
        if (redisTemplate.opsForValue().get(todayKey)==null){
            redisTemplate.opsForValue().set(todayKey,result.size(),EXPIRE_TIME,TimeUnit.DAYS);
        }else {
            // 计数++
            redisTemplate.opsForValue().increment(todayKey,result.size());
        }
        // 返回结果


        return result;
    }

    @Override
    public String getLongUrl(String shortUrl) {
        // 拼接key值
        String todayKey = TODAY_USED_SIZE + ConvertDateUtils.getDate();
        // 短链接使用次数+1
        redisTemplate.opsForValue().increment(USED_SIZE,1);
        // 记录当天的次数 有效时间为9天
        // 初始化
        if (redisTemplate.opsForValue().get(todayKey)==null){
            redisTemplate.opsForValue().set(todayKey,1,EXPIRE_TIME,TimeUnit.DAYS);
        }else {
            // 计数++
            redisTemplate.opsForValue().increment(todayKey,1);
        }
        // 查询redis
        String longUrl = (String) redisTemplate.opsForValue().get(shortUrl);
//        if (longUrl == null){
//            // 查询数据库
//            longUrl = mapDOMapper.selectByShortUrl(shortUrl);
//        }
        return longUrl;
    }




}
